
import {
  getexpro,
} from '@/api/message/list'
import { MessageBox, Message } from 'element-ui';
//封装获取：当前日期+星期几
let getDateWeek = (etpye) => {
  const date = new Date();
  let wk = date.getDay()
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month; //月小于10，加0
  day = day < 10 ? "0" + day : day; //day小于10，加0
  let weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  let week = weeks[wk];
  if (etpye == 'Y') {
    return `${year}`
  }
  if (etpye == 'Ym') {
    return `${year}-${month}`
  }
  if (etpye == 'Ymd') {
    return `${year}-${month}-${day}`
  }
  return `${year}-${month}-${day}` + ' ' + week;
}
//获取当前时间段区间
let getDateTimes = (etype) => {
  const date = new Date();
  let wk = date.getDay()
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month; //月小于10，加0
  day = day < 10 ? "0" + day : day; //day小于10，加0
  let fromData;
  //昨天
  if (etype == 'yestoday') {
    let today_start_date = GetDateStr(-1) + ' 00:00:00';
    let today_end_date = GetDateStr(-1) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  // if(etype=='today'){
  //     let today_start_date=GetDateStr(0)+' 00:00:00';
  //     let today_end_date=GetDateStr(0)+' 23:59:59';
  //     fromData={"start_date":today_start_date,"end_date":today_end_date}
  //     return fromData;
  // }
  //近一周
  if (etype == 'last7day' || etype == '1') {
    let today_start_date = GetDateStr(-6) + ' 00:00:00';
    let today_end_date = GetDateStr(0) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  //近一个月
  if (etype == '2') {
    let today_start_date = GetDateStr(-30) + ' 00:00:00';
    let today_end_date = GetDateStr(0) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  //近三个月
  if (etype == '3') {
    let today_start_date = GetDateStr(-90) + ' 00:00:00';
    let today_end_date = GetDateStr(0) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  //近六个月
  if (etype == '4') {
    let today_start_date = GetDateStr(-180) + ' 00:00:00';
    let today_end_date = GetDateStr(0) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  //近十二个月
  if (etype == '5') {
    let today_start_date = GetDateStr(-360) + ' 00:00:00';
    let today_end_date = GetDateStr(0) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  //今日
  if (etype == 'today') {
    let today_start_date = GetDateStr(0) + ' 00:00:00';
    let today_end_date = GetDateStr(0) + ' 23:59:59';
    fromData = { "start_date": today_start_date, "end_date": today_end_date }
    return fromData;
  }
  //本月
  if (etype == 'month') {
    let month_start_date = GetYearMonthStr(0) + `-01 ` + '00:00:00';
    let month_end_date = GetYearMonthStr(1) + `-01 ` + '00:00:00';
    fromData = { "start_date": month_start_date, "end_date": month_end_date }
    return fromData;
  }
  //本年
  if (etype == 'year') {
    let month_start_date = `${year}-01-01 ` + '00:00:00';
    let month_end_date = `${year + 1}-01-01 ` + '00:00:00';
    fromData = { "start_date": month_start_date, "end_date": month_end_date }
    return fromData;
  }
}

let GetDateStr = (AddDayCount) => {
  var dd = new Date();
  dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;//获取当前月份的日期
  var d = dd.getDate();
  return y + "-" + m + "-" + d;
}
let GetYearMonthStr = (monthType) => {
  var dd = new Date();
  var year = dd.getFullYear();
  var month = dd.getMonth() + 1;//获取当前月份的日期
  //当月
  if (monthType == 0) {
    month = month > 9 ? month : '0' + month;
    return year + '-' + month;
  }
  //下个月
  if (monthType == 1) {
    var year2 = year;
    var month2 = parseInt(month) + 1;
    if (month == 13) {
      year2 = parseInt(year) + 1;
      month2 = 1;
    }
    month2 = month2 > 9 ? month2 : '0' + month2;
    return year2 + '-' + month2;
  }
  return year + '-' + month;
}
//数字千位分隔符
let numFormat = (num) => {
  num = num.toString().split(".");  // 分隔小数点
  var arr = num[0].split("").reverse();  // 转换成字符数组并且倒序排列
  var res = [];
  for (var i = 0, len = arr.length; i < len; i++) {
    if (i % 3 === 0 && i !== 0) {
      res.push(",");   // 添加分隔符
    }
    res.push(arr[i]);
  }
  res.reverse(); // 再次倒序成为正确的顺序
  if (num[1]) {  // 如果有小数的话添加小数部分
    res = res.join("").concat("." + num[1]);
  } else {
    res = res.join("");
  }
  return res;
}

let bigNumberTransform = (value) => {
  if (value < 0) return 0;
  const newValue = ['', '', '']
  let fr = 1000
  let num = 3
  let text1 = ''
  let fm = 1
  while (value / fr >= 1) {
    fr *= 10
    num += 1
    // console.log('数字', value / fr, 'num:', num)
  }
  if (num <= 4) { // 千
    newValue[0] = parseInt(value / 1000) + ''
    newValue[1] = '千'
  } else if (num <= 8) { // 万
    text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
    // tslint:disable-next-line:no-shadowed-variable
    fm = text1 === '万' ? 10000 : 10000000
    if (value % fm === 0) {
      newValue[0] = parseInt(value / fm) + ''
    } else {
      newValue[0] = parseFloat(value / fm).toFixed(3) + ''
    }
    newValue[1] = text1
  } else if (num <= 16) { // 亿
    text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
    text1 = (num - 8) / 4 > 1 ? '万亿' : text1
    text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
    // tslint:disable-next-line:no-shadowed-variable
    fm = 1
    if (text1 === '亿') {
      fm = 100000000
    } else if (text1 === '千亿') {
      fm = 100000000000
    } else if (text1 === '万亿') {
      fm = 1000000000000
    } else if (text1 === '千万亿') {
      fm = 1000000000000000
    }
    if (value % fm === 0) {
      newValue[0] = parseInt(value / fm) + ''
    } else {
      newValue[0] = parseFloat(value / fm).toFixed(3) + ''
    }
    newValue[1] = text1
  }
  if (value < 1000) {
    newValue[0] = value + ''
    newValue[1] = ''
  }
  return newValue.join('')
}

// 入参 fmt-格式 date-日期,如：dateFormat("YYYY-mm-dd HH:MM:SS", date)
let dateFormat = (fmt, date) => {
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
}
//导出execl数据
let downloadCallback = (fromData, length) => {
  let title = ''
  if (length > 0) {
    title = '是否导出当前选中数据'
  } else {
    title = '是否导出全部数据'
  }
  MessageBox.confirm(title, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    getexpro(fromData).then((res) => {
      if (res.status == 200) {
        let dispositionStr = res.headers["content-disposition"];
        if (dispositionStr == null || dispositionStr === "") {
          Message({
            message: '导出失败',
            type: 'error'
          });
          return;
        }
        let dispositionArr = dispositionStr.split(";");
        let fileName = decodeURIComponent(dispositionArr[1]);
        fileName = fileName.split("=")[1];
        if (fileName.startsWith("utf-8''")) {
          fileName = fileName.replace("utf-8''", "")
        }
        let blob = new Blob([res.data], {
          type: "application/vnd.ms-excel;charset=utf-8"
        });
        if ('msSaveOrOpenBlob' in navigator) {
          window.navigator.msSaveOrOpenBlob(blob, fileName); // 针对浏览器
        }
        else {
          const elink = document.createElement('a'); // 创建a标签
          elink.download = fileName;
          elink.target = '_blank';
          elink.style.display = 'none';
          // 创建一个指向blob的url，这里就是点击可以下载文件的根结
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); //移除链接
          document.body.removeChild(elink); //移除a标签
        }
      } else {
        Message({
          message: '导出失败',
          type: 'error'
        });
      }
    })
  }).catch(() => {
    Message({
      type: 'info',
      message: '取消导出'
    });
  });
}

export {
  getDateWeek,
  getDateTimes,
  numFormat,
  dateFormat,
  bigNumberTransform,
  GetDateStr,
  downloadCallback,
}